package marvin.client.ui;

import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import marvin.client.core.SubscriptionClient;

public class JF_Main extends JFrame {

	private static final long serialVersionUID = 1L;

	private SubscriptionClient client;

	private javax.swing.JButton jbConnect, jbDisconnect;
	private javax.swing.JLabel jlName;
	private javax.swing.JLabel jlPort;
	private javax.swing.JLabel jlServer;
	private javax.swing.JTextField jtfName;
	private javax.swing.JTextField jtfPort;
	private javax.swing.JTextField jtfServer;
    private javax.swing.JLabel jlSound;
    private javax.swing.JLabel jlSoundData;
    private javax.swing.JLabel jlCurrentFile;

    private String marvin_host = System.getProperty("marvin.host");
    
	public JF_Main() {
		setTitle("Ma.R.V.In client");
		initComponents();
	}

	/*private void initComponents() {

        jbConnect = new javax.swing.JButton();
        jtfName = new javax.swing.JTextField("Laptop");
        jlName = new javax.swing.JLabel();
        jlServer = new javax.swing.JLabel();
        jtfServer = new javax.swing.JTextField(12);
        jlPort = new javax.swing.JLabel();
        jtfPort = new javax.swing.JTextField(12);
        jbDisconnect = new javax.swing.JButton();
        jlSound = new javax.swing.JLabel();
        jlSoundData = new javax.swing.JLabel();
        jlCurrentFile = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ma.R.V.In client");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jbConnect.setText("Connect");
        jbConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConnectActionPerformed(evt);
            }
        });

        jlName.setText("Name:");

        jlServer.setText("Server:");
        jtfServer.setText("FIXE");

        jlPort.setText("Port:");
        jtfPort.setText("4242");

        jbDisconnect.setText("Disconnect");
        jbDisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDisconnectActionPerformed(evt);
            }
        });

        jbDisconnect.setEnabled(false);
        jlSound.setText("Sound:");

        Container content = getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new GridLayout(5, 0));

        content.add(generatePanel(new Component[]{jlServer, jtfServer}));
        content.add(generatePanel(new Component[]{jlPort, jtfPort}));
        content.add(generatePanel(new Component[]{jlName, jtfName}));
        content.add(generatePanel(new Component[]{jbConnect, jbDisconnect}));

        pack();
	}
	
	private JPanel generatePanel(Component comps[]){
		JPanel p = new JPanel();
		p.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		p.setAlignmentY(JPanel.LEFT_ALIGNMENT);
		p.setBackground(Color.white);

		for(Component c: comps)
			p.add(c);

		return p;
	}
	*/
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */	private void initComponents() {

        jbConnect = new javax.swing.JButton();
        jtfName = new javax.swing.JTextField("Laptop");
        jlName = new javax.swing.JLabel();
        jlServer = new javax.swing.JLabel();
        jtfServer = new javax.swing.JTextField(marvin_host);
        jlPort = new javax.swing.JLabel();
        jtfPort = new javax.swing.JTextField("4242");
        jbDisconnect = new javax.swing.JButton();
        jlSound = new javax.swing.JLabel();
        jlSoundData = new javax.swing.JLabel();
        jlCurrentFile = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ma.R.V.In client");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jbConnect.setText("Connect");
        jbConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConnectActionPerformed(evt);
            }
        });

        jlName.setText("Name:");

        jlServer.setText("Server:");

        jlPort.setText("Port:");

        jbDisconnect.setText("Disconnect");
        jbDisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDisconnectActionPerformed(evt);
            }
        });
        jbDisconnect.setEnabled(false);

        jlSound.setText("Sound:");

        jlSoundData.setText("50");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlPort)
                            .addComponent(jlName))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfName, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfPort, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlServer)
                        .addGap(18, 18, 18)
                        .addComponent(jtfServer)))
                .addGap(18, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbDisconnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbConnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlSound)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlSoundData)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlServer)
                    .addComponent(jtfServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbConnect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlPort)
                    .addComponent(jtfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbDisconnect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlName)
                    .addComponent(jtfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlSound)
                    .addComponent(jlSoundData))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

	private void formWindowClosing(java.awt.event.WindowEvent evt) {
		try {
			if(client != null)
				client.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void jbDisconnectActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			client.disconnect();
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void jbConnectActionPerformed(java.awt.event.ActionEvent evt) {
		client = new SubscriptionClient(Integer.parseInt(jtfPort.getText()), jtfServer.getText());
		client.sendId(jtfName.getText());
		
		jbConnect.setEnabled(false);
		jtfName.setEnabled(false);
		jtfServer.setEnabled(false);
		jtfPort.setEnabled(false);
		jbDisconnect.setEnabled(true);
	}

	public void setSoundValue(int parseInt) {
		jlSoundData.setText(""+parseInt);	
	}
	
	public void setCurrentFile(String current) {
		jlCurrentFile.setText(current);	
	}

	public void displayMessage(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}
}
